package org.ricks.action.rpc;

import org.ricks.ioc.anno.Action;
import org.ricks.ioc.utils.Logger;
import org.ricks.net.action.anno.ActionMethod;
import org.ricks.net.action.context.FlowContext;
import org.ricks.net.action.context.head.RpcHeadMeta;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Action
public class RpcAction {

    private final static ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
    private final static AtomicInteger a = new AtomicInteger();
    static {
//        s.scheduleAtFixedRate(() -> Logger.info("最后收到的消息ID " + a.get()),5L,5L, TimeUnit.SECONDS);
    }


    @ActionMethod(isRpc = true, command = 10)
    public void rpcTest (FlowContext<RpcHeadMeta> flowContext) {
//        Logger.info("data字节流：{}", Arrays.toString(flowContext.getData()));
//        Logger.info("rpc..................." + new String(flowContext.getData()));
        int msgId = flowContext.headMeta.rpcRequestId();
        if(msgId == 1) {
            Logger.info("服务器收到第一条客户端消息 msgid:{}", msgId);
        }

        a.set(msgId);
        if(msgId == 100000) {
            Logger.info("服务器收到最后一条客户端消息 msgid:{}", msgId);

        }
        if(msgId % 10000== 0) {
            Logger.info("收到消息 msgId:{}", msgId);
        }
//        Logger.info("收到消息 msgId:{}", msgId);

        flowContext.rpcResponse("我市rpc，玛德");
    }
}
